﻿NAMFox.ns(function() { with (NAMFox) {

name("Color Tests");

//////////////////////////////////////////////
// Helper Methods
//////////////////////////////////////////////

var colors = [
    {
        red: 255,
        green: 128,
        blue: 55,
        hue: 16,
        saturation: 255,
        lightness: 155,
        hex: "#FF8037"
    },
    {
        red: 197,
        green: 93,
        blue: 159,
        hue: 228,
        saturation: 121,
        lightness: 145,
        hex: "#C55D9F"
    },
    {
        red: 106,
        green: 197,
        blue: 23,
        hue: 65,
        saturation: 202,
        lightness: 110,
        hex: "#6AC517"
    },
    {
        red: 10,
        green: 76,
        blue: 40,
        hue: 104,
        saturation: 196,
        lightness: 43,
        hex: "#0A4C28"
    },
    {
        red: 5,
        green: 43,
        blue: 139,
        hue: 159,
        saturation: 238,
        lightness: 72,
        hex: "#052B8B"
    },
    {
        red: 217,
        green: 144,
        blue: 250,
        hue: 200,
        saturation: 234,
        lightness: 197,
        hex: "#D990FA"
    },
    {
        red: 0,
        green: 0,
        blue: 0,
        hue: 0,
        saturation: 0,
        lightness: 0,
        hex: "#000000"
    },
    {
        red: 255,
        green: 255,
        blue: 255,
        hue: 0,
        saturation: 0,
        lightness: 255,
        hex: "#FFFFFF"
    }
];

function colorTest(name, action) {
    test(name, function() {
        for (var i = 0; i < colors.length; ++i) {
            action(colors[i]);
        }
    });
}

//////////////////////////////////////////////
// Test Cases
//////////////////////////////////////////////

// Issue 68
colorTest("Translates RGB to HSL", function(colorSpec) {
    var color = Color.fromRgb(colorSpec.red, colorSpec.green, colorSpec.blue);
    
    ok(
        Math.abs(colorSpec.hue - color.hue) <= 5,
        "The hues must be within 5 points of each other. Expected: " + colorSpec.hue + ". Actual: " + color.hue + ".");
    ok(
        Math.abs(colorSpec.saturation - color.saturation) <= 5,
        "The saturations must be within 5 points of each other. Expected: " + colorSpec.saturation + ". Actual: " + color.saturation + ".");
    ok(
        Math.abs(colorSpec.lightness - color.lightness) <= 5,
        "The lightnesses must be within 5 points of each other. Expected: " + colorSpec.lightness + ". Actual: " + color.lightness + ".");
});

colorTest("Translates HSL to RGB", function(colorSpec) {
    var color = Color.fromHsl(colorSpec.hue, colorSpec.saturation, colorSpec.lightness);
                
    ok(
        Math.abs(colorSpec.red - color.red) <= 5,
        "The reds must be within 5 points of each other. Expected: " + colorSpec.red + ". Actual: " + color.red + ".");
    ok(
        Math.abs(colorSpec.green - color.green) <= 5,
        "The greens must be within 5 points of each other. Expected: " + colorSpec.green + ". Actual: " + color.green + ".");
    ok(
        Math.abs(colorSpec.blue - color.blue) <= 5,
        "The blues must be within 5 points of each other. Expected: " + colorSpec.blue + ". Actual: " + color.blue + ".");
});

colorTest("Translates Hex to RGB/HSL", function(colorSpec) {
    var color = Color.fromHex(colorSpec.hex);
                
    ok(
        Math.abs(colorSpec.red - color.red) <= 5,
        "The reds must be within 5 points of each other. Expected: " + colorSpec.red + ". Actual: " + color.red + ".");
    ok(
        Math.abs(colorSpec.green - color.green) <= 5,
        "The greens must be within 5 points of each other. Expected: " + colorSpec.green + ". Actual: " + color.green + ".");
    ok(
        Math.abs(colorSpec.blue - color.blue) <= 5,
        "The blues must be within 5 points of each other. Expected: " + colorSpec.blue + ". Actual: " + color.blue + ".");
    ok(
        Math.abs(colorSpec.hue - color.hue) <= 2,
        "The hues must be within 2 points of each other. Expected: " + colorSpec.hue + ". Actual: " + color.hue + ".");
    ok(
        Math.abs(colorSpec.saturation - color.saturation) <= 2,
        "The saturations must be within 2 points of each other. Expected: " + colorSpec.saturation + ". Actual: " + color.saturation + ".");
    ok(
        Math.abs(colorSpec.lightness - color.lightness) <= 2,
        "The lightnesses must be within 2 points of each other. Expected: " + colorSpec.lightness + ". Actual: " + color.lightness + ".");
});

colorTest("Translates RGB to Hex", function(colorSpec) {
    var color1 = Color.fromRgb(colorSpec.red, colorSpec.green, colorSpec.blue);
    var color2 = Color.fromHex(colorSpec.hex);
    
    equals(
        colorSpec.hex.toUpperCase(),
        color1.toHex().toUpperCase(),
        "The hex codes must match.");
    equals(
        colorSpec.hex.toUpperCase(),
        color2.toHex().toUpperCase(),
        "The hex codes must match.");
});

}});